﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAO;
using DTO;
using System.Text.RegularExpressions;


namespace Presentation
{
    public partial class SearchForm : Form
    {
        public SearchForm()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            List<MaDTO> ds = new List<MaDTO>();

            ds = SearchDAO.TimKiem(txtSearch.Text.Trim());
        }
        private void LoadGridDuLieu(List<DuLieuDTO> dsDULIEU)
        {
            gridDuLieu.Rows.Clear();
            //List<DuLieuDTO> dsDULIEU = new List<DuLieuDTO>();
            //DuLieuDTO iDuLieu = new DuLieuDTO();

            //iDuLieu.TenDiaDiem = "Quán Cây Dừa";
            //iDuLieu.DichVu = "Quán ăn";
            //iDuLieu.Duong = "Lý Thường Kiệt";
            //iDuLieu.QuanHuyen = "11";
            //iDuLieu.SoNha = "345";
            //iDuLieu.Phuong = "3";

            //dsDULIEU.Add(iDuLieu);
            for (int i = 0; i < dsDULIEU.Count; i++)
            {
                gridDuLieu.Rows.Add(dsDULIEU[i].TenDiaDiem, dsDULIEU[i].DichVu, dsDULIEU[i].SoNha, dsDULIEU[i].Duong, dsDULIEU[i].QuanHuyen,dsDULIEU[i].ChuThich );
            }
        }

        public String xoaDau(String str)
        {
            Regex regex = new Regex(@"\p{IsCombiningDiacriticalMarks}+");
            string strFormD = str.Normalize(System.Text.NormalizationForm.FormD);
            return regex.Replace(strFormD, String.Empty).Replace('\u0111', 'd').Replace('\u0110', 'D');
        }

        public String chuanHoa(String str)
        {
            str = xoaDau(str);
            for (int i = 0; i < str.Length; i++)
            {
                if (str[i] == ' ')
                {
                    str = str.Remove(i, 1);
                    i--;
                }
            }
            return str;
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            String iSearchString = txtSearch.Text.Trim();
            iSearchString = iSearchString.ToLower();
            String iStandardSearchString = chuanHoa(iSearchString);

            List<MaDTO> dsMa = new List<MaDTO>();
            dsMa = SearchDAO.TimKiem(iStandardSearchString);

            List<DuLieuDTO> dsDuLieu = new List<DuLieuDTO>();
            dsDuLieu = SearchDAO.TraCuuDuLieu(dsMa);

            LoadGridDuLieu(dsDuLieu);

        }
    }
}
